<?php 
defined('IN_TS') or die('Access Denied.');

//用户是否登录
$userid = aac('user')->isLogin();

switch($ts){

	//添加评论
	case "do":
	
		$lessonid	= intval($_POST['lessonid']);
		$content	= tsClean($_POST['content']);
		
		if($content==''){
			tsNotice('没有任何内容是不允许你通过滴^_^');
		}
		
		$commentid = $new['study']->create('study_lesson_comment',array(
		
			'lessonid'			=> $lessonid,
			'userid'			=> $userid,
			'content'	=> $content,
			'addtime'		=> time(),
		
		));
		
		
		//统计评论数
		$count_comment = $new['study']->findCount('study_lesson_comment',array(
			'lessonid'=>$lessonid,
		));
		
		$new['study']->update('study_lesson',array(
			'lessonid'=>$lessonid,
		),array(
			'count_comment'=>$count_comment,
		));


		header("Location: ".tsUrl('study','lesson',array('id'=>$lessonid)));

	
		break;
		
	//删除评论
	case "delete":
		
		$commentid = intval($_GET['commentid']);
		
		$strComment = $new['study']->find('study_topics_comments',array(
			'commentid'=>$commentid,
		));
		
		$strLesson = $new['study']->find('study_topics',array(
			'topicid'=>$strComment['topicid'],
		));
		
		$strStudy = $new['study']->find('study',array(
			'studyid'=>$strLesson['studyid'],
		));
		
		if($strLesson['userid']==$userid || $strStudy['userid']==$userid || $TS_USER['isadmin']==1){
			
			$new['study']->delComment($commentid);
			
			//统计
			$db->query("update ".dbprefix."study_topics set count_comment=count_comment-1 where topicid='".$strComment['topicid']."'");
		}
		
		//跳转回到话题页
		header("Location: ".tsUrl('study','topic',array('id'=>$strComment['topicid'])));
		
		break;

}